/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desktopapplication1;

/**
 *
 * @author Lora
 */
public class Informer {

    private Ip ip;
    private Subnet subnet;
    private Integer numberSubnet;
    private Integer numberHost;
    private Integer numberBitsHost;
    private Integer numberBitsSubnet;
    private String informe;

    public Informer() {
        ip = new Ip();
        subnet = new Subnet();
        numberHost = new Integer(0);
        numberBitsSubnet = new Integer(0);
        numberBitsHost = new Integer(0);
    }

    public Informer(Ip ip, Subnet subnet) {
        this();
        this.ip = ip;
        this.subnet = subnet;
    }

    public void calculateNumberBitsSubnet() {
        numberBitsSubnet = subnet.getNumeroBits();
    }

    public void calculateNumberSubnet() {
        calculateNumberBitsSubnet();
        numberSubnet = Integer.valueOf(new Double(Math.pow(2, numberBitsSubnet)).intValue()) - 2;

    }

    public void calculateNumberBitsHost() {
        calculateNumberBitsSubnet();
        numberBitsHost = 32 - numberBitsSubnet;
    }

    public void calculateNumberHost() {
        calculateNumberBitsHost();
        numberHost = Integer.valueOf(new Double(Math.pow(2, numberBitsHost)).intValue()) - 2;
    }

    public String informar() {
        informe = new String();

        Ip red = new Ip();
        Ip firstHost = new Ip();
        Ip lastHost = new Ip();
        Ip broadcast = new Ip();

        calculateNumberSubnet();
        calculateNumberHost();

        if (!ip.getValido()) {
            informe = informe + "Este IP no es valido\n\n";
            return informe;
        }
        if (!subnet.getValido()) {
            informe = informe + "Esta mascara no es valida\n\n";
            return informe;
        }

        informe = informe + "Numero de Redes:\t[" + numberSubnet + "]\t\t" + "Numero de Hosts por Red:\t[" + numberHost + "]\n\n";
        informe = informe + "Numero Bits Subnet:\t[" + numberBitsSubnet + "]\t\t" + "Numero Bits Host:\t[" + numberBitsHost + "]\n\n";

        red = new Ip(ip.getNumeroCompleto() & subnet.getNumeroCompleto());
        firstHost = new Ip(red.getNumeroCompleto() + 1);
        lastHost = new Ip(red.getNumeroCompleto() + numberHost);
        broadcast = new Ip(red.getNumeroCompleto() + numberHost + 1);

        informe = informe + "[Network] " + red.getFirstNumber().toString() + "." +
                red.getSecondNumber().toString() + "." +
                red.getThirdNumber().toString() + "." +
                red.getFourthNumber().toString() + "\t";

        informe = informe + "[Primer Host] " + firstHost.getFirstNumber().toString() + "." +
                firstHost.getSecondNumber().toString() + "." +
                firstHost.getThirdNumber().toString() + "." +
                firstHost.getFourthNumber().toString() + "\t";

        informe = informe + "[Ultimo Host] " + lastHost.getFirstNumber().toString() + "." +
                lastHost.getSecondNumber().toString() + "." +
                lastHost.getThirdNumber().toString() + "." +
                lastHost.getFourthNumber().toString() + "\t";

        informe = informe + "[Broadcast] " + broadcast.getFirstNumber().toString() + "." +
                broadcast.getSecondNumber().toString() + "." +
                broadcast.getThirdNumber().toString() + "." +
                broadcast.getFourthNumber().toString() + "\n";

        if (ip.getNumeroCompleto().compareTo(red.getNumeroCompleto()) == 0) {
            informe = informe + "\n\n Este IP solo indica la red, no es valida para host";
        } else if (ip.getNumeroCompleto().compareTo(broadcast.getNumeroCompleto()) == 0) {
            informe = informe + "\n\n Este IP es solo para broadcast, no es valida para host";
        }
        return informe;

    }
}
